<template>
	<view class="page">
		<u--form labelPosition="left" labelWidth="230rpx" ref="uForm" :model="form" :rules='formRules'>
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">患者信息</view>
			<view class="card-container">
				<!-- <u-cell-group :border="false">
					<view class="patient-info">
						<view>
							<text class="patient-title">姓名：</text>
							<text class="patient-name">{{userInfo.memberName}}</text>
						</view>
						<view>
							<text class="patient-title">性别：</text>
							<text class="patient-name">{{userInfo.memberGender==1?'男':'女'}}</text>
						</view>
						<view>
							<text class="patient-title">年龄：</text>
							<text class="patient-name">{{userInfo.MemberAge}}20</text>
						</view>

					</view>
					<view>
						<text class="patient-title">身份证：</text>
						<text class="patient-name">130127201039203302{{userInfo.memberIdcard}}</text>
					</view>
					<view>
						<text class="patient-title">主诉：</text>
						<text class="patient-name">{{userInfo.mainSuit}}</text>
					</view>
					<view>
						<text class="patient-title">出院诊断：</text>
						<text class="patient-name">{{userInfo.assessmentContent}}</text>
					</view>
					<view>
						<text class="patient-title">随访要求：</text>
						<text class="patient-name">{{userInfo.followUpRequire}}</text>
					</view>
					<view>
						<text class="patient-title">随访要求次数：</text>
						<text class="patient-name">{{userInfo.followUpTimesTotal}}</text>
					</view> -->

				<u-cell title="姓名" :value="userInfo.memberName"></u-cell>
				<u-cell title="性别" :value="userInfo.memberGender==1?'男':'女'"></u-cell>
				<u-cell title="身份证" :value="userInfo.memberIdcard"></u-cell>
				<u-cell title="出生日期" :value="userInfo.memberBirth.split(' ')[0]"></u-cell>
				<u-cell title="年龄" :value="userInfo.memberAge"></u-cell>
				<u-cell title="主诉" :value="userInfo.mainSuit"></u-cell>
				<u-cell title="出院诊断" :value="userInfo.assessmentContent"></u-cell>
				<u-cell title="随访要求" :value="userInfo.followUpRequire"></u-cell>
				<u-cell title="随访要求次数" :value="userInfo.followUpTimesTotal"></u-cell>
				<u-cell title="下转日期" :value="userInfo.dischargeTime"></u-cell>
				<u-cell title="出院医生" :value="userInfo.dischargeDoctor" :border="false"></u-cell>
				</u-cell-group>
			</view>
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">填报信息</view>
			<view class="card-container">
				<u-form-item label="收缩压" prop="patientSBP" borderBottom>
					<u--input v-model="form.patientSBP" border="none" placeholder="请填写血压">
						<u--text text="mmHg" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="form.patientSBP>139" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="form.patientSBP!==''&&form.patientSBP<90?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="舒张压" prop="patientDBP" borderBottom>
					<u--input v-model="form.patientDBP" border="none" placeholder="请填写血压">
						<u--text text="mmHg" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="form.patientDBP>80" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="form.patientDBP!==''&&form.patientDBP<60?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="体温" prop="patientBodyTemperature" borderBottom>
					<u--input v-model="form.patientBodyTemperature" border="none" placeholder="请填写体温">
						<u--text text="℃" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="form.patientBodyTemperature>37" slot="right" name="arrow-upward"
						color="red"></u-icon>
					<u-icon v-show="form.patientBodyTemperature!==''&&form.patientBodyTemperature<36?true:false"
						slot="right" name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="脉搏" prop="patientPulse;" borderBottom>
					<u--input v-model="form.patientPulse" border="none" placeholder="请填写脉搏次数">
						<u--text text="次/分" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="form.patientPulse>100" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="form.patientPulse!==''&&form.patientPulse<60?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="呼吸" prop="patientBreathe" borderBottom>
					<u--input v-model="form.patientBreathe" border="none" placeholder="请填写呼吸次数">
						<u--text text="次/分" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="form.patientBreathe>20" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="form.patientBreathe!==''&&form.patientBreathe<12?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="本次病情描述" prop="patientHeath" borderBottom>
					<u--textarea v-model="form.patientHeath" placeholder="请输入内容"></u--textarea>
				</u-form-item>
				<u-form-item label="备注" prop="notes" borderBottom>
					<u--textarea v-model="form.notes" placeholder="请输入内容"></u--textarea>
				</u-form-item>
				<u-form-item label="现场照片" prop="followUpPhoto" borderBottom>
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10"></u-upload>
				</u-form-item>
			</view>
		</u--form>
		<view class="button-group-list">
			<view class="save-button">
				<u-button @click="onSubmit(0)" type="warning" text="暂存" shape="circle"></u-button>
			</view>
			<view class="save-button">
				<u-button @click="onSubmit(1)" type="primary" text="提交" shape="circle"></u-button>
			</view>
		</view>
		<!--随访时间选择-->
		<u-calendar showLunar :show="tranTimeShow" @confirm="tranTimeConfirm" @close='tranTimeShow=false' showLunar
			closeOnClickOverlay>
		</u-calendar>
	</view>
</template>

<script>
	import config from '@/common/config'
	import {
		saveFollowUp
	} from '@/common/api/followup/add/add.js'
	import {
		formatDate
	} from '@/util/utils.js';
	export default {
		data() {
			return {
				followUpPhoto:'',
				tranTimevalue: Number(new Date()),
				tranTimeShow: false,
				fileList1: [],
				form: {
					id: '',
					transferId: '',
					followUpTime: "",
					patientBodyTemperature: '',
					patientPulse: '',
					patientBreathe: '',
					patientSBP: '',
					patientDBP: '',
					patientHeath: "",
					followUpPhoto: "",
					notes: ""
				},
				userInfo: {},
				formRules: {
					followUpTime: [
						// 必填验证
						{
							type: 'string',
							required: true,
							message: '请选择日期',
							trigger: ['blur', 'change'],
						},
						// 额外添加长度验证规则
						// {
						// 	validator: (rule, value, callback) => {
						// 		const regex = /^\d{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$/;
						// 		if (value.length !== 8 || !regex.test(value)) {
						// 			callback(new Error('请输入格式为 20250101 的8位纯数字日期'));
						// 		} else {
						// 			callback();
						// 		}
						// 	},
						// 	trigger: 'blur'
						// }
					],
					'phoneNumber': {
						type: 'number',
						message: '请填写数字',
						trigger: ['blur', 'change']
					}
				},
			};
		},
		onLoad(info) {
			this.userInfo = info
		},
		methods: {
			// 随访时间确认
			tranTimeConfirm(e) {
				this.form.followUpTime = e[0]
				this.tranTimeShow = false
			},
			onSubmit(val) {
				this.$refs.uForm.validate().then(valid => {
					if (valid) {
						this.fileList1.forEach(item => {
							this.followUpPhoto+= ','+item.url.replace(config.imgURl,'')
						});
						this.form.followUpPhoto = this.followUpPhoto.replace(/^,/, '')
						this.form.followUpTime = this.form.followUpTime.replace(/(\d{4})(\d{2})(\d{2})/,
							'$1-$2-$3')
						this.form.transferId = this.userInfo.transferId
						const params = {
							num: val,
							form: this.form
						}
						// return
						saveFollowUp(params).then(res => {
							if (res.code === 1) {
								uni.showToast({
									title: '提交成功',
									icon: 'success'
								});
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1000);
							}
						})
					}
				})

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					console.log('lists', lists)
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "成功",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "/ygt-api/file/addFile",
						filePath: url,
						name: "file",
						success: (res) => {

							const data = JSON.parse(res.data)
							resolve(config.imgURl + data.message);

						},
					});
				});
			},
		},
	}
</script>

<style lang="scss" scoped>
	.patient-info {
		display: flex;
		justify-content: space-between;

	}

	.patient-title {
		color: $text-main-color;
		font-size: $ft-size-md;
		font-weight: inherit;


	}

	.patient-name {
		color: $text-content-color;
		font-size: $ft-size-base;
	}

	.page {
		padding: 0 0 calc(64rpx + 120rpx + env(safe-area-inset-bottom)) 0;
		box-sizing: border-box;
	}

	.form-card-title {
		padding: 0 42rpx 16rpx;
		font-size: $ft-size-md;
		color: $text-desc-color;
	}

	.card-container {
		padding: 0 20rpx;
	}

	.button-group-list {
		position: fixed;
		left: 32rpx;
		bottom: calc(32rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: space-around;
		box-sizing: border-box;
		padding: 0 10rpx;
		width: 686rpx;
		height: 100rpx;
		border-radius: 1000rpx;
		background-color: #fff;
		box-shadow: 0 0 15rpx 0 rgba(0, 0, 0, 0.05);
		z-index: 10;

		.save-button {
			width: 200rpx;
		}

		.save-entry-button {
			width: 380rpx;
		}
	}
</style>